import keyboard
import win32gui
import win32con

# 用于保存被隐藏的窗口句柄
hidden_window = None

def find_edge_window():
    """查找 Edge 浏览器窗口"""
    def enum_windows_callback(hwnd, windows):
        if win32gui.IsWindowVisible(hwnd):
            class_name = win32gui.GetClassName(hwnd)
            title = win32gui.GetWindowText(hwnd)
            # Edge 窗口的类名通常是 'ApplicationFrameWindow' 或 'Chrome_WidgetWin_1'（新版本）
            # 并且标题中包含 "Microsoft Edge"
            if ("Microsoft Edge" in title or "Edge" in title) and (
                "ApplicationFrameWindow" in class_name or "Chrome_WidgetWin_1" in class_name
            ):
                windows.append(hwnd)
        return True

    windows = []
    win32gui.EnumWindows(enum_windows_callback, windows)
    return windows[0] if windows else None

def toggle_edge_window():
    global hidden_window

    if hidden_window:
        # 恢复窗口
        win32gui.ShowWindow(hidden_window, win32con.SW_RESTORE)
        win32gui.SetForegroundWindow(hidden_window)
        print("Edge 窗口已恢复")
        hidden_window = None
    else:
        hwnd = find_edge_window()
        if hwnd:
            # 隐藏窗口（从任务栏和桌面都消失）
            win32gui.ShowWindow(hwnd, win32con.SW_HIDE)
            print("Edge 窗口已隐藏")
            hidden_window = hwnd
        else:
            print("未找到 Edge 窗口")

def main():
    print("老板键已启动：按 Alt+Q 隐藏/恢复 Edge 窗口")
    print("按 Ctrl+C 退出程序")

    # 注册快捷键
    keyboard.add_hotkey('alt+q', toggle_edge_window)

    try:
        keyboard.wait('esc')  # 按 Esc 退出（可选），也可以用 Ctrl+C
    except KeyboardInterrupt:
        print("\n程序已退出")

if __name__ == "__main__":
    main()